package com.lidong.framework.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.lidong.framework.mybatis.query.ConditionQuery;
import com.lidong.framework.util.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Grid extends QueryPojo {
	private int code;
	private String msg;
	//每页条数
	private int limit = 10;
	//当前第几页
	private int page = 1;
	//总共几页
	private int pageTotal;
	//查询集合
	private List<?> data;
	//总记录数
	private int count;
	//排序字段
	private String sort;
	//排序方式
	private String order;
	//排序字段对应数据库名称
	private Map<String, String> sortColumn = new HashMap<>();

	private Integer firstRow0;
	private Integer firstRow1;
	private Integer endRow;

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getPageTotal() {
		return pageTotal;
	}
	public void setPageTotal(int pageTotal) {
		this.pageTotal = pageTotal;
	}
	public List<?> getData() {
		if(data == null){
			data = new ArrayList();
		}
		return data;
	}
	public void setData(List<?> list) {
		this.data = list;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
		if(limit > 0){
			this.pageTotal = (count + limit - 1) / limit;
		}
	}
	/**
	 * 查询数据起始行，从0开始，支持mysql
	 * @return
	 */
	@JsonIgnore
	public int getFirstRow0(){
		if(firstRow0 == null){
			firstRow0 = ((page - 1) * limit);
		}
		return firstRow0;
	}
	/**
	 * 查询数据起始行，从1开始，支持sqlserver
	 * @return
	 */
	@JsonIgnore
	public int getFirstRow1(){
		if(firstRow1 == null){
			firstRow1 = ((page - 1) * limit) + 1;
		}
		return firstRow1;
	}
	@JsonIgnore
	public int getEndRow(){
		if(endRow == null){
			endRow = page * limit;
		}
		return endRow;
	}

	/**
	 * 排序字段
	 * @return
	 */
	@JsonIgnore
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}

	/**
	 * 排序方式asc/desc
	 * @return
	 */
	@JsonIgnore
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	/**
	 * 添加排序字段和数据库表字段对应关系
	 * @param field java bean字段
	 * @param column 数据库中对应的字段
	 */
	public void addSortColumn(String field, String column){
		sortColumn.put(field, column);
	}
	/**
	 * 获取排序字段实际字段名
	 * @param field
	 * @return
	 */
	@JsonIgnore
	private String getSortColumn(String field){
		String column = sortColumn.get(field);
		if(StringUtil.isBlank(column)){
			return field;
		}
		return column;
	}
	/**
	 *
	 */
	protected void initSortColumn(){

	}
	/**
	 * 过滤特殊字符，防止sql注入
	 * @param keyword 要处理字符串
	 * @return 处理后的字符串
	 */
	protected String colateKeyword(String keyword) {
		if (keyword == null) {
			return null;
		}
		keyword = keyword.replaceAll("'", "");
		keyword = keyword.replaceAll("\\*", "");
		keyword = keyword.replaceAll("--", "");
		keyword = keyword.replaceAll(" ", "");
		keyword = keyword.replaceAll("\\(", "");
		keyword = keyword.replaceAll("\\)", "");
		return keyword;
	}

	/**
	 * 构建排序语句
	 * @return
	 */
	protected String buildSortOrder() {
		if(StringUtil.isBlank(sort) || StringUtil.isBlank(order)){
			return null;
		}
		initSortColumn();
		String[] fields = this.colateKeyword(sort).split(",");
		String[] orders = this.colateKeyword(order).split(",");
		StringBuilder sortOrder = new StringBuilder();
		for (int i = 0; i < fields.length; i++) {
			if(i > 0){
				sortOrder.append(",");
			}
			sortOrder.append(getSortColumn(fields[i]));
			sortOrder.append(" ");
			sortOrder.append(orders[i]);
		}
		return sortOrder.toString();
	}

	@Override
	public ConditionQuery buildConditionQuery() {
		ConditionQuery query = super.buildConditionQuery();
		query.addParam("firstRow0", this.getFirstRow0());
		query.addParam("firstRow1", this.getFirstRow1());
		query.addParam("endRow", this.getEndRow());
		query.addParam("limit", this.getLimit());
		query.addParam("orderCol", buildSortOrder());
		return query;
	}
}
